package com.venutech.gongan.api;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.service.ISysUserService;
import com.venutech.gongan.entity.TbOpWorkOrder;
import com.venutech.gongan.service.ITbOpWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 运维Controller
 * 
 * @author linxb
 * @date 2023-09-19
 */
@Api(value = "运维管理", tags = "运维管理")
@RestController
@RequestMapping("/gongan/work")
public class TbOpWorkOrderController extends BaseController
{

    @Autowired
    private ITbOpWorkOrderService tbOpWorkOrderService;
    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询运维列表
     */
    @ApiOperation("查询运维列表分页")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbOpWorkOrder tbOpWorkOrder)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbOpWorkOrder.setRepairMan(loginUser.getUserId());
        List<TbOpWorkOrder> list = tbOpWorkOrderService.selectTbOpWorkOrderList(tbOpWorkOrder);
        return getDataTable(list);
    }
    /**
     * 查询运维列表
     */
    @ApiOperation("查询运维列表")
    @GetMapping("/getOpWorkOrder")
    @ResponseBody
    public AjaxResult getOpWorkOrder(TbOpWorkOrder tbOpWorkOrder)
    {
        List<TbOpWorkOrder> list = tbOpWorkOrderService.selectTbOpWorkOrderList(tbOpWorkOrder);
        return AjaxResult.success(list);
    }


    /**
     * 查询运维列表
     */
//    @ApiOperation("查询运维工单审批列表")
//    @GetMapping("/tbOpWorkOrderList")
//    @ResponseBody
//    public TableDataInfo tbOpWorkOrderList(TbOpWorkOrder tbOpWorkOrder)
//    {
//        startPage();
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        tbOpWorkOrder.setResponseMan(loginUser.getUserId());
//        List<TbOpWorkOrder> list = tbOpWorkOrderService.selectTbOpWorkOrderList(tbOpWorkOrder);
//        return getDataTable(list);
//    }

    /**
     * 导出运维列表
     */
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(@RequestBody TbOpWorkOrder TbOpWorkOrder)
//    {
//        List<TbOpWorkOrder> list = TbOpWorkOrderService.selectTbOpWorkOrderList(TbOpWorkOrder);
//        ExcelUtil<TbOpWorkOrder> util = new ExcelUtil<TbOpWorkOrder>(TbOpWorkOrder.class);
//        return util.exportExcel(list, "运维数据");
//    }



    /**
     * 新增保存运维
     */
    @ApiOperation("新增保存运维")
    @PostMapping("/addTbOpWorkOrder")
    @ResponseBody
    public AjaxResult addSave(@RequestBody TbOpWorkOrder tbOpWorkOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbOpWorkOrder.setRepairMan(loginUser.getUserId());
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        tbOpWorkOrder.setRepairTime(date);
        tbOpWorkOrder.setFaultTime(date);
        return toAjax(tbOpWorkOrderService.insertTbOpWorkOrder(tbOpWorkOrder));
    }


    /**
     * 修改保存运维
     */
    @ApiOperation("修改保存运维")
    @PostMapping("/editTbOpWorkOrder")
    @ResponseBody
    public AjaxResult editSave(@RequestBody TbOpWorkOrder tbOpWorkOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        TbOpWorkOrder opWork=new TbOpWorkOrder();
        //BeanUtils.copyProperties(tbOpWorkOrder,opWork);
        opWork.setRepairMan(loginUser.getUserId());
        List<TbOpWorkOrder> opWorkList=tbOpWorkOrderService.selectTbOpWorkOrderList(opWork);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        tbOpWorkOrder.setRepairTime(date);
        tbOpWorkOrder.setFaultTime(date);
        return toAjax(tbOpWorkOrderService.updateTbOpWorkOrder(tbOpWorkOrder));


    }
    /**
     * 运维工单审批
     */
    @ApiOperation("运维工单审批")
    @PostMapping("/approveTbOpWorkOrder")
    @ResponseBody
    public AjaxResult approveTbOpWorkOrder(@RequestBody TbOpWorkOrder tbOpWorkOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        TbOpWorkOrder opWork=new TbOpWorkOrder();
        opWork.setResponseMan(loginUser.getUserId());
        List<TbOpWorkOrder> opWorkList=tbOpWorkOrderService.selectTbOpWorkOrderList(opWork);
        if(opWorkList.size()>0){
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            tbOpWorkOrder.setResponseTime(date);
            return toAjax(tbOpWorkOrderService.approveTbOpWorkOrder(tbOpWorkOrder));
        }else{
            return AjaxResult.error("该工单只能由指定审批人审批");
        }



    }

    /**
     * 删除运维
     */
//    @ApiOperation("删除运维")
    @PostMapping( "/removeTbOpWorkOrder")
    @ResponseBody
    public AjaxResult remove(@RequestBody String ids)
    {
        return toAjax(tbOpWorkOrderService.deleteTbOpWorkOrderByOpWorkIds(ids));
    }

    /**
     * 删除运维
     */
    @ApiOperation("删除运维")
    @GetMapping( "/deleteTbOpWorkOrderByOpWorkId/{id}")
    @ResponseBody
    public AjaxResult deleteTbOpWorkOrderByOpWorkId(@PathVariable Long id)
    {
        return toAjax(tbOpWorkOrderService.deleteTbOpWorkOrderByOpWorkId(id));
    }
}
